package com.haitaoit.chengquan.module.center_service.activity.service_order;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.center.activity.yuyue.YuyueEvaluateActivity;
import com.haitaoit.chengquan.module.center_service.model.ServiceFuwuDetailModel;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.DialogUtil;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.CircleImageView;
import com.haitaoit.chengquan.view.QRatingBar;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ServiceFuwuDetailActivity extends BaseActivity {
    @ViewInject(R.id.ser_detail_evaLL)
    LinearLayout ser_detail_evaLL;
    @ViewInject(R.id.fuwu_detail_buttonLL)
    LinearLayout fuwu_detail_buttonLL;
    @ViewInject(R.id.fuwu_detail_time1)
    TextView fuwu_detail_time1;
    @ViewInject(R.id.fuwu_detail_time2)
    TextView fuwu_detail_time2;
    @ViewInject(R.id.fuwu_detail_time3)
    TextView fuwu_detail_time3;
    @ViewInject(R.id.fuwu_detail_time3_1)
    TextView fuwu_detail_time3_1;
    @ViewInject(R.id.fuwu_detail_time4)
    TextView fuwu_detail_time4;
    @ViewInject(R.id.fuwu_detail_time5)
    TextView fuwu_detail_time5;
    @ViewInject(R.id.fuwu_detail_state)
    TextView fuwu_detail_state;
    @ViewInject(R.id.submit_finish)
    TextView submit_finish;
    @ViewInject(R.id.submit_order)
    TextView submit_order;
    @ViewInject(R.id.update_order)
    TextView update_order;
    @ViewInject(R.id.detele_order)
    TextView detele_order;
    @ViewInject(R.id.to_eva)
    TextView to_eva;
    @ViewInject(R.id.fuwu_detail_price)
    TextView fuwu_detail_price;
    @ViewInject(R.id.accept_name)
    TextView accept_name;
    @ViewInject(R.id.telphone)
    TextView telphone;
    @ViewInject(R.id.area_address)
    TextView area_address;
    @ViewInject(R.id.user_remark)
    TextView user_remark;
    @ViewInject(R.id.service_time)
    TextView service_time;
    @ViewInject(R.id.titile_img)
    ImageView titile_img;
    @ViewInject(R.id.order_title)
    TextView order_title;
    @ViewInject(R.id.yuyue_content)
    TextView yuyue_content;
    @ViewInject(R.id.yuyue_money)
    TextView yuyue_money;
    @ViewInject(R.id.center_img)
    CircleImageView center_img;
    @ViewInject(R.id.eva_name)
    TextView eva_name;
    @ViewInject(R.id.eva_content)
    TextView eva_content;
    @ViewInject(R.id.eva_addtime)
    TextView eva_addtime;
    @ViewInject(R.id.eva_star)
    QRatingBar eva_star;


    private String order_no, title_type;
    private ServiceFuwuDetailModel model;
    private Dialog dlg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_service_fuwu_detail);
        setTitle_V("订单详情");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


        title_type = getIntent().getStringExtra("title_type");
        order_no = getIntent().getStringExtra("order_no");
    }

    @Override
    protected void onResume() {
        if ("1".equals(title_type)) {
            httpGetmer_service_order_model(Constant.Getmer_service_order_model);
        } else if ("2".equals(title_type)) {
            httpGetmer_service_order_model(Constant.Getmer_service_jcgz_order_model);
        }
        super.onResume();
    }

    @OnClick({R.id.telphone, R.id.submit_finish, R.id.submit_order, R.id.update_order, R.id.detele_order,
            R.id.to_eva})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.telphone:// 电话
                startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + model.getTelphone())));
                break;
            case R.id.submit_finish:// 确认完成
                dlg = DialogUtil.showInfoDialog1(this, "提示", "您确定该订单服务已完成吗？", "确定", "还没有", new BackCall() {
                    public void deal(int which, Object... obj) {
                        switch (which) {
                            case R.id.confirm:// 确定
                                httpGetmer_service_complete_order(order_no);
                                break;
                        }
                        super.deal(which);
                    }
                });
                dlg.setCanceledOnTouchOutside(true);
                dlg.show();
                break;
            case R.id.submit_order:// 确认订单
                dlg = DialogUtil.showInfoDialog1(this, "提示", "您要确认订单吗？", "确定", "取消", new BackCall() {
                    public void deal(int which, Object... obj) {
                        switch (which) {
                            case R.id.confirm:// 确定
                                httpGetmer_service_confirm_order(order_no);
                                break;
                        }
                        super.deal(which);
                    }
                });
                dlg.setCanceledOnTouchOutside(true);
                dlg.show();
                break;
            case R.id.update_order:// 修改订单
                Dialog dlg = DialogUtil.ShowUpdateOrderDialog(ServiceFuwuDetailActivity.this, new BackCall() {
                    @Override
                    public void deal(int which, Object... obj) {
                        String price = (String) obj[0];
                        switch (which) {
                            case R.id.submit_price:
                                ToastUtils.showToast("price" + price);
                                httpGetmer_service_update_order_amount(order_no, price);
                                break;
                        }
                        super.deal(which, obj);
                    }
                });
                dlg.setCanceledOnTouchOutside(true);
                dlg.show();

                DisplayMetrics metric = new DisplayMetrics();
                this.getWindowManager().getDefaultDisplay().getMetrics(metric);
                int width = metric.widthPixels; // 屏幕宽度（像素）
                WindowManager.LayoutParams params = dlg.getWindow().getAttributes();
                params.width = width;
                params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
                dlg.getWindow().setAttributes(params);
                break;
            case R.id.detele_order:// 删除订单
                dlg = DialogUtil.showInfoDialog1(this, "提示", "您确定要删除该服务订单吗？", "确定", "取消", new BackCall() {
                    public void deal(int which, Object... obj) {
                        switch (which) {
                            case R.id.confirm:// 确定
                                httpGetdel_order(order_no);
                                break;
                        }
                        super.deal(which);
                    }
                });
                dlg.setCanceledOnTouchOutside(true);
                dlg.show();
                break;
            case R.id.to_eva:// 评价需求商
                Intent intent = new Intent(this, YuyueEvaluateActivity.class);
                intent.putExtra("order_no", order_no);
                intent.putExtra("type", "2");
                startActivity(intent);
                break;
        }
        super.onClick(v);
    }

    private void httpGetmer_service_order_model(String url) {
        final List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String result = arg0.result.toString();
                try {
                    JSONObject json = new JSONObject(result);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    Gson gson = new Gson();
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            break;
                        case 0:// 成功
                            model = gson.fromJson(Response, ServiceFuwuDetailModel.class);
                            ser_detail_evaLL.setVisibility(View.GONE);
                            fuwu_detail_time3.setVisibility(View.GONE);
                            fuwu_detail_time3_1.setVisibility(View.GONE);
                            fuwu_detail_time4.setVisibility(View.GONE);
                            fuwu_detail_time5.setVisibility(View.GONE);
                            fuwu_detail_buttonLL.setVisibility(View.VISIBLE);
                            /**
                             * status:订单状态1未支付2已支付(is_service:1显示服务完成2不显示)
                             * 3已完成(is_evaluation_mer:1显示评价2不显示)4已取消(删除订单)
                             *
                             * status:订单状态1待确认(显示确认订单)
                             * 2已确认(is_update_order:1显示修改金额2不显示)
                             * 3已支付(is_service:1显示完成按钮2不显示)
                             * 4已完成(is_evaluation_mer:服务商是否评价1否2是)5已取消(显示删除)
                             * */
                            if ("1".equals(title_type)) {
                                if (model.getStatus().equals("1")) {
                                    fuwu_detail_state.setText("未支付");
                                    fuwu_detail_buttonLL.setVisibility(View.GONE);
                                } else if (model.getStatus().equals("2")) {
                                    fuwu_detail_state.setText("已支付");
                                    if (model.getIs_service().equals("1")) {
                                        submit_finish.setVisibility(View.VISIBLE);
                                    } else {
                                        fuwu_detail_buttonLL.setVisibility(View.GONE);
                                    }
                                    fuwu_detail_time3.setVisibility(View.VISIBLE);
                                } else if (model.getStatus().equals("3")) {
                                    fuwu_detail_state.setText("已完成");
                                    // is_evaluation_mer:服务商是否评价1否2是 is_evaluation:需求商是否评价1否2是
                                    if (model.getIs_evaluation_mer().equals("1")) {
                                        to_eva.setVisibility(View.VISIBLE);
                                    } else {
                                        fuwu_detail_buttonLL.setVisibility(View.GONE);
                                    }
                                    if (model.getIs_evaluation().equals("1")) {
                                        ser_detail_evaLL.setVisibility(View.GONE);
                                    } else {
                                        ser_detail_evaLL.setVisibility(View.VISIBLE);
                                        ImageLoader.getInstance().displayImage(model.getService_evaluation().getHead_portrait(), center_img);
                                        eva_name.setText(model.getService_evaluation().getName());
                                        eva_content.setText(model.getService_evaluation().getContent());
                                        eva_addtime.setText(model.getService_evaluation().getAddtime());
                                        eva_star.setRating(Float.parseFloat(model.getService_evaluation().getScore()));
                                    }
                                    fuwu_detail_time3.setVisibility(View.VISIBLE);
                                    fuwu_detail_time5.setVisibility(View.VISIBLE);
                                } else if (model.getStatus().equals("4")) {
                                    fuwu_detail_state.setText("已取消");
                                    detele_order.setVisibility(View.VISIBLE);
                                }
                            } else if ("2".equals(title_type)) {
                                if (model.getStatus().equals("1")) {
                                    fuwu_detail_state.setText("待确认");
                                    submit_order.setVisibility(View.VISIBLE);
                                    fuwu_detail_price.setTextColor(getResources().getColor(R.color.text_gray));
                                } else if (model.getStatus().equals("2")) {
                                    fuwu_detail_state.setText("已确认");

                                    if (model.getIs_update_order().equals("1")) {
                                        update_order.setVisibility(View.VISIBLE);
                                    } else {
                                        fuwu_detail_buttonLL.setVisibility(View.GONE);
                                    }
                                    fuwu_detail_time3_1.setVisibility(View.VISIBLE);
                                    fuwu_detail_time4.setVisibility(View.VISIBLE);
                                    fuwu_detail_price.setTextColor(getResources().getColor(R.color.text_gray));
                                } else if (model.getStatus().equals("3")) {
                                    fuwu_detail_state.setText("已支付");
                                    if (model.getIs_service().equals("1")) {
                                        submit_finish.setVisibility(View.VISIBLE);
                                    } else {
                                        fuwu_detail_buttonLL.setVisibility(View.GONE);
                                    }
                                    fuwu_detail_time3_1.setVisibility(View.VISIBLE);
                                    fuwu_detail_time4.setVisibility(View.VISIBLE);
                                    fuwu_detail_time3.setVisibility(View.VISIBLE);
                                } else if (model.getStatus().equals("4")) {
                                    fuwu_detail_state.setText("已完成");
                                    // is_evaluation_mer:服务商是否评价1否2是 is_evaluation:需求商是否评价1否2是
                                    if (model.getIs_evaluation_mer().equals("1")) {
                                        to_eva.setVisibility(View.VISIBLE);
                                    } else {
                                        fuwu_detail_buttonLL.setVisibility(View.GONE);
                                    }
                                    if (model.getIs_evaluation().equals("1")) {
                                        ser_detail_evaLL.setVisibility(View.GONE);
                                    } else {
                                        ser_detail_evaLL.setVisibility(View.VISIBLE);
                                        ImageLoader.getInstance().displayImage(model.getService_evaluation().getHead_portrait(), center_img);
                                        eva_name.setText(model.getService_evaluation().getName());
                                        eva_content.setText(model.getService_evaluation().getContent());
                                        eva_addtime.setText(model.getService_evaluation().getAddtime());
                                        eva_star.setRating(Float.parseFloat(model.getService_evaluation().getScore()));
                                    }
                                    fuwu_detail_time3.setVisibility(View.VISIBLE);
                                    fuwu_detail_time5.setVisibility(View.VISIBLE);
                                } else if (model.getStatus().equals("5")) {
                                    fuwu_detail_state.setText("已取消");
                                    detele_order.setVisibility(View.VISIBLE);
                                }
                            }

                            accept_name.setText("联系人：" + model.getAccept_name());
                            telphone.setText(model.getTelphone());
                            area_address.setText("服务地址：" + model.getArea() + model.getAddress());
                            if (model.getUser_remark().equals(""))
                                user_remark.setText("无");
                            else
                                user_remark.setText(model.getUser_remark());
                            service_time.setText(model.getService_time());
                            ImageLoader.getInstance().displayImage(model.getTitile_img(), titile_img);
                            order_title.setText(model.getTitle());
                            yuyue_content.setText("分类：" + model.getType());
                            yuyue_money.setText("¥ " + model.getMoney());
                            fuwu_detail_price.setText("¥ " + model.getOrder_amount());

                            fuwu_detail_time1.setText("预约编号：" + model.getOrder_no());
                            fuwu_detail_time2.setText("创建时间：" + model.getAddtime());
                            fuwu_detail_time3_1.setText("确认时间：" + model.getPay_confirm_order_timetime());
                            fuwu_detail_time4.setText("价格评估：" + model.getUpdate_time());
                            fuwu_detail_time3.setText("付款时间：" + model.getPay_time());
                            fuwu_detail_time5.setText("完成时间：" + model.getConfirm_time());
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 服务-服务商确认订单(服务商)
    private void httpGetmer_service_confirm_order(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getmer_service_confirm_order, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            if ("1".equals(title_type)) {
                                httpGetmer_service_order_model(Constant.Getmer_service_order_model);
                            } else if ("2".equals(title_type)) {
                                httpGetmer_service_order_model(Constant.Getmer_service_jcgz_order_model);
                            }
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 服务-服务商确认订单(服务商)
    private void httpGetmer_service_update_order_amount(String order_no, String money) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        list.add(new NameValuePairSign("money", money));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("money", money);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getmer_service_update_order_amount, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            if ("1".equals(title_type)) {
                                httpGetmer_service_order_model(Constant.Getmer_service_order_model);
                            } else if ("2".equals(title_type)) {
                                httpGetmer_service_order_model(Constant.Getmer_service_jcgz_order_model);
                            }
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 服务-服务商确认完成服务(服务商)
    private void httpGetmer_service_complete_order(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getmer_service_complete_order, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            if ("1".equals(title_type)) {
                                httpGetmer_service_order_model(Constant.Getmer_service_order_model);
                            } else if ("2".equals(title_type)) {
                                httpGetmer_service_order_model(Constant.Getmer_service_jcgz_order_model);
                            }
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 删除订单
    private void httpGetdel_order(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getdel_order, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
